package com.hqyj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.ClassesMapper;
import com.hqyj.dao.DepartmentMapper;
import com.hqyj.dao.UserMapper;
import com.hqyj.pojo.Classes;
import com.hqyj.pojo.Department;
import com.hqyj.pojo.User;
import com.hqyj.service.ClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;




@Service
public class ClassesServiceImpl implements ClassesService {
    @Autowired
    ClassesMapper classesMapper;
    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public Classes selectById(Long id) {
        return classesMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Classes> selectAllClasses() {
        return classesMapper.selectAllClasses();
    }

    @Override
    public int insert(Classes classes) {
        return classesMapper.insertSelective(classes);
    }

    @Override
    public int deleteLogic(Long id) { return classesMapper.deleteLogic(id); }

    @Override
    public HashMap<String, Object> select(Classes classes) {
        HashMap<String,Object> map = new HashMap<String, Object>();
        //1 设置分页参数：页码和条数
        PageHelper.startPage(classes.getPage(),classes.getRow());
        //2 查询结果集合
        List<Classes> list = classesMapper.selectAllClasses();
        // 3 创建分页对象
        PageInfo<Classes> pageInfo = new PageInfo<Classes>(list);


        System.out.println("--------------------------");
        System.out.println("总条数="+pageInfo.getTotal());
        System.out.println("总页数="+pageInfo.getPages());
        System.out.println("上一页="+pageInfo.getPrePage());
        System.out.println("下一页="+pageInfo.getNextPage());
        System.out.println("首页="+pageInfo.getNavigateFirstPage());
        System.out.println("尾页="+pageInfo.getNavigateLastPage());
        System.out.println("当前页="+classes.getPage());
        System.out.println("--------------------------");

        map.put("total",pageInfo.getTotal());
        map.put("pages",pageInfo.getPages());
        map.put("endPage",pageInfo.getNavigateLastPage());
        map.put("curPage",classes.getPage());
        map.put("data",pageInfo.getList());

        System.out.println(map);

        return map;
    }

    @Override
    public HashMap<String, Object> delete(Long id) {
        long a = classesMapper.deleteLogic(id);
        HashMap<String,Object> map =new HashMap<String, Object>();
        if (a>0){
            map.put("info","删除成功");
        }else {
            map.put("info","删除失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> insertNew(Classes classes) {
        classes.setIsDelete(0);
        int a = classesMapper.insert(classes);
        HashMap<String,Object> map = new HashMap<String, Object>();
        if (a>0){
            map.put("info","增加成功");
        }else {
            map.put("info","增加失败");
        }
        return map;
    }

    @Override
    public List<Department> findDepartment(Department department) {
        List<Department> departmentList = departmentMapper.selecAll();
        return departmentList;
    }

    @Override
    public List<User> findUser() {
        List<User> userList = userMapper.selectTeacher();
        return userList;
    }

    @Override
    public HashMap<String, Object> update(Classes classes) {
        classes.setIsDelete(0);
        int a = classesMapper.updateByPrimaryKey(classes);
        HashMap<String,Object> map = new HashMap<String, Object>();
        if (a>0){
            map.put("info","修改成功");
        }else {
            map.put("info","修改失败");
        }
        return map;
    }


}